<template>
  <div>
    <a-modal
      ok-text="确认"
      cancel-text="取消"
      :maskClosable="false"
      :title="title + '仓库'"
      :width="modalWidth"
      @cancel="handleClose"
      :destroyOnClose="true"
      @ok="onSubmit"
      :visible="visible"
      :confirmLoading="confirmLoading"
      footer=""
    >
      <template>
        <div class="modalBut">
          <a-button @click="handleClose">
            取消
          </a-button>
          <a-button @click="onSubmit" type="primary">确认</a-button>
        </div>
      </template>
      <a-spin :spinning="confirmLoading">
        <a-form-model ref="ruleForm" :model="model" :rules="rules">
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="仓库名称"
            prop="wareHouseName"
            hasFeedback
          >
            <a-input
              placeholder="请输入仓库名称"
              v-model.trim="model.wareHouseName"
            />
          </a-form-model-item>

          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="仓库负责人"
            prop="managerId"
            hasFeedback
          >
            <a-select
              :open="false"
              :defaultOpen="false"
              mode="multiple"
              :allowClear="true"
              placeholder="仓库负责人"
              v-model.trim="model.managerId"
              @dropdownVisibleChange="handlePerson"
              @change="handlePersonChange"
            >
              <a-select-option
                v-for="item in tableUserOption"
                :key="item.id"
                :value="item.id"
              >
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="仓库编号"
            prop="wareHouseNumber"
            hasFeedback
          >
            <a-input
              placeholder="请输入仓库编号"
              v-model.trim="model.wareHouseNumber"
            />
          </a-form-model-item>

          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            label="仓库地址"
            prop="wareHouseAddress"
            hasFeedback
          >
            <a-input
              placeholder="请输入仓库地址"
              v-model.trim="model.wareHouseAddress"
            />
          </a-form-model-item>
        </a-form-model>
      </a-spin>
      <table-Modal
        ref="tableForm"
        :columns="user"
        :tableKey="userTableKey"
        :treeKey="userTreeKey"
        @tableSelect="tableSelectUser"
        :replaceFields="userReplaceFields"
        :searchKey="userSearchKey"
        :name="userName"
        :filename="userFilename"
      ></table-Modal>
    </a-modal>
  </div>
</template>

<script>
import api from "@/api/apiServer";
import TableModal from "@/components/TableSelectModal";
import { user } from "@/unit/tableUser.js";
export default {
  components: { TableModal },
  props: {
    treeData: {
      type: Array,
    },
  },
  data() {
    return {
      title: "", // 标题
      visible: false, // 状态、
      modalWidth: 652, // 抽屉宽度
      model: {},
      replaceFields: {
        children: "wareHouseTypeList",
        title: "wareHouseTypeName",
        key: "id",
        value: "id",
      },
      show: true, //根据菜单类型，动态显示隐藏表单元
      上级部门: "角色名称",
      menuLabel: "菜单名称",
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      iconChooseVisible: false,
      validateStatus: "",
      tableUserOption: [],
      user,
      userReplaceFields: {
        children: "childList",
        title: "departmentName",
        key: "id",
        value: "id",
      },
      userTreeKey: "DepartmentTree",
      userTableKey: "UserList",
      userSearchKey: "departmentId",
      userName: "name",
      userFilename: "UserModal",
    };
  },
  computed: {
    rules() {
      return {
        wareHouseName: [
          { required: true, message: "必填仓库名称", trigger: "change" },
        ],
        managerId: [
          { required: true, message: "必填仓库负责人", trigger: "change" },
        ],
        wareHouseNumber: [
          { required: true, message: "必填仓库编号", trigger: "change" },
        ],
        wareHouseAddress: [
          { required: true, message: "必填仓库地址", trigger: "change" },
        ],
      };
    },
  },
  methods: {
    // 新增
    modalAdd() {
      this.modalEdit({});
    },
    // 编辑
    modalEdit(record) {
      this.resetScreenSize();
      this.visible = true;
      this.getUserAllList();
      this.model = Object.assign({}, record);
    },
    // 获取负责人数据
    async getUserAllList() {
      let params = { pageNumber: 1, pageSize: 99999, deleteFlag: "N" };
      const res = await api.UserList(params);
      this.tableUserOption = res.data.data;
    },
    onChangeMenuType() {},
    onSubmit() {
      const that = this;
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          that.handleSubmit();
        } else {
          return false;
        }
      });
    },
    // 关闭
    handleClose() {
      this.visible = false;
      this.$refs.ruleForm.resetFields();
    },
    async handleSubmit() {
      let res;
      this.confirmLoading = true;
      if (this.model.id) {
        res = await api.wareHouseEdit(this.model); //修改
      } else {
        res = await api.wareHouseAdd(this.model); //新增
      }
      if (res.code && res.code !== 200) {
        this.$message.error(res.message);
        this.confirmLoading = false;
        return;
      }
      this.$message.success(res.message);
      this.confirmLoading = false;
      this.$emit("ok");
      this.visible = false;
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth;
      if (screenWidth < 500) {
        this.modalWidth = screenWidth;
      } else {
        this.modalWidth = 652;
      }
    },
    handlePerson() {
      this.$refs.tableForm.title = "人员";
      this.$refs.tableForm.modalAdd();
      if (this.model.managerId) {
        let arr = this.model.managerId.toString().split(",");
        if (arr.length > 0) {
          let newArr = arr.map((item) => {
            return Number(item);
          });
          this.$refs.tableForm.selectedRowKeys = newArr;
        } else {
          this.$refs.tableForm.selectedRowKeys = [];
        }
      }
    },
    handlePersonChange(value) {
      console.log(value, "变化");
      if (value.length > 0) {
        this.model.managerId = value.toString();
      } else {
        delete this.model.managerId;
      }
    },
    tableSelectUser(result, tableUserOption) {
      this.tableUserOption = tableUserOption;
      this.$set(this.model, "managerId", result[0].id);
      this.$set(this.model, "wareHouseManager", result[0].name);
      this.$refs.ruleForm.clearValidate(["managerId"]);
    },
  },
};
</script>

<style></style>
